#include "entitymodel.h"

EntityModel::EntityModel(QObject *parent) :
    QAbstractTableModel(parent)
{
}

int EntityModel::rowCount(const QModelIndex & parent) const{
    return m_ents.size();
}

int EntityModel::columnCount(const QModelIndex & parent) const{
    return 6;
}

QVariant EntityModel::data ( const QModelIndex & index, int role) const{

    if(role==Qt::DisplayRole||role==Qt::EditRole){
        int row = index.row();
        int column=index.column();
        AED_Entity ent = m_ents[row];
        QVariant value;
        switch(column){
        case 0:
            value = ent.IsSelected();
            break;
        case 1:
            value = ent.GetArchtype()->GetName();
            break;
        case 2:
            value = ent.m_pos_x;
            break;
        case 3:
            value = ent.m_pos_y;
            break;
        case 4:
            value = ent.m_rot;
            break;
        case 5:
            value = ent.m_size;
            break;
        default:
            return QVariant(QVariant::Invalid);
        }
        return value;
    }
    return QVariant(QVariant::Invalid);
}

QVariant EntityModel::headerData ( int section, Qt::Orientation orientation, int role) const{
    if(orientation==Qt::Horizontal){
        if (role==Qt::DisplayRole){
            QVariant value;
            switch(section){
            case 0:
                value = "Selected";
                break;
            case 1:
                value = "Archtype";
                break;
            case 2:
                value = "Pos X";
                break;
            case 3:
                value = "Pos Y";
                break;
            case 4:
                value = "Rotation";
                break;
            case 5:
                value = "Size";
                break;
            default:
                return QVariant(QVariant::Invalid);
            }
            return value;
        }else{
            return QVariant(QVariant::Invalid);
        }
    }else{
        if (role==Qt::DisplayRole){
            QVariant h(QVariant::Int);
            h=section+1;
            return h;
        }else{
            return QVariant(QVariant::Invalid);
        }
    }
}

bool EntityModel::setData(const QModelIndex &index, const QVariant &value, int role){
    int row = index.row();
    int column = index.column();
     AED_Entity &ent = m_ents[row];

    if(index.isValid() && role==Qt::EditRole){
        switch(column){
        case 0:
            ent.SetSelected(value.toBool());
            break;
        case 2:
            ent.m_pos_x = value.toDouble();
            break;
        case 3:
            ent.m_pos_y = value.toDouble();
            break;
        case 4:
            ent.m_rot = value.toDouble();
            break;
        case 5:
            ent.m_size = value.toDouble();
            break;
        default:
            return false;
        }


        emit dataChanged(index,index);
        emit layoutChanged();
        return true;
    }

    return false;
}

Qt::ItemFlags EntityModel::flags(const QModelIndex &index) const{
    Qt::ItemFlags flags;
    if(index.column()<=1)
        flags = Qt::ItemIsEnabled|Qt::ItemIsSelectable;
    else
        flags = Qt::ItemIsEnabled|Qt::ItemIsSelectable|Qt::ItemIsEditable;
    return flags;
}

AED_Entity* EntityModel::AddEntity(AED_Archtype *arch){
    m_ents.append(AED_Entity(arch));
    emit layoutChanged();

    return &(m_ents.last());
}

void EntityModel::CopySelectedEntities(){
    QList<AED_Entity*> selected;
    for(int i=0;i<m_ents.size();i++){
        if(m_ents[i].IsSelected()){
            selected.append(&(m_ents[i]));
        }
    }

    for(int i=0;i<selected.size();i++){
        AED_Entity* ent = AddEntity(selected.at(i)->GetArchtype());
        AED_Entity* sent = selected[i];
        sent->SetSelected(false);
        ent->SetSelected(true);
        ent->m_pos_x = sent->m_pos_x;
        ent->m_pos_y = sent->m_pos_y;
        ent->m_rot = sent->m_rot;
        ent->m_size = sent->m_size;
    }
}

void EntityModel::Save(QTextStream &file){
    for(int i=0;i<m_ents.size();i++){
        m_ents[i].Save(file);
    }
}

void EntityModel::Save(QDomDocument &doc, QDomElement &header){
    for(int i=0;i<m_ents.size();i++){
        m_ents[i].Save(doc,header);
    }
}

bool EntityModel::Load(QDomElement &header, ArchtypeModel &archmodel){
    QDomNodeList ents = header.elementsByTagName("entity");
    for(int i = 0;i < ents.count(); i++){
        QDomElement eelem = ents.at(i).toElement();
        QString name = eelem.attribute("name");
        float rot = eelem.attribute("rot").toFloat();
        float size = eelem.attribute("size").toFloat();
        float pos_x = eelem.attribute("pos_x").toFloat();
        float pos_y = eelem.attribute("pos_y").toFloat();
        AED_Archtype* arch = archmodel.GetArchtype(name);
        AED_Entity ent(arch);

        ent.m_pos_x = pos_x;
        ent.m_pos_y = pos_y;
        ent.m_rot = rot;
        ent.m_size = size;
        m_ents.append(ent);

    }
    emit layoutChanged();
    return true;
}

void EntityModel::UpdateSelection(QItemSelection selected,QItemSelection deselected){
    QModelIndexList indexes = selected.indexes();
    for(int i=0;i<indexes.size();i++){
        QModelIndex index = indexes[i];
        int row = index.row();
        m_ents[row].SetSelected(true);
    }
    indexes = deselected.indexes();
    for(int i=0;i<indexes.size();i++){
        QModelIndex index = indexes[i];
        int row = index.row();
        m_ents[row].SetSelected(false);
    }

    emit SelectionUpdated();
}

void EntityModel::EraseSelectedEntities(){
    QList<AED_Entity>::iterator it=m_ents.begin();

    while(it!=m_ents.end()){
        if((*it).IsSelected()){
            it = m_ents.erase(it);
        }else{
            ++it;
        }
    }

    emit layoutChanged();
}

void EntityModel::EraseEntitiesByArchtype(AED_Archtype *arch){
    QList<AED_Entity>::iterator it=m_ents.begin();

    while(it!=m_ents.end()){
        if((*it).GetArchtype()==arch){
            it = m_ents.erase(it);
        }else{
            ++it;
        }
    }

    emit layoutChanged();
}

int EntityModel::CountEntitiesByArchtype(AED_Archtype *arch){
    int count=0;

    QList<AED_Entity>::iterator it=m_ents.begin();

    while(it!=m_ents.end()){
        if((*it).GetArchtype()==arch){
            count++;
        }

        ++it;

    }

    return count;
}

void EntityModel::ClearImage(const QString name){

    for(int i=0;i<m_ents.size();i++){
        AED_RenderComp *comp = m_ents[i].GetArchtype()->GetRenderComp();
        if(comp->GetImage()){
            if(comp->GetImage()->GetName()==name)
                comp->SetImage(0);
        }

    }
}

void EntityModel::Clean(){
    m_ents.clear();
    emit layoutChanged();
}


